package com.cavas.domain.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.cavas.domain.bean.Finance;
import com.cavas.domain.bean.Membre;
import com.cavas.domain.bean.Pret;

@Repository
public class DaoPretImpl implements DaoPret {

	@Autowired
	SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory fct){
		sessionFactory = fct;
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public void addPret(Pret pret, Membre membre) {
		Session session = sessionFactory.getCurrentSession();		
		List<Finance> liste  = session.createQuery("from  Finance").list();
		String build = "";
		for(int i = 0; i < liste.size(); i++){
			Finance fi = liste.get(i);
			String st = fi.getFinanceId()+":"+fi.getFdr();
			build = build + st;
			if(i + 1 != liste.size())
				build = build + ".";
		}
		pret.setFinances(build);
		session.save(pret);	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Pret> allPret() {
		return sessionFactory.getCurrentSession().createQuery("from Pret").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Pret> allPret(String nom) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Pret clt where clt.nom= :nom");
		query.setParameter("nom", nom);
		return  query.list();
	}
	
	/*@Override
	public String getPreteur(Pret pret) {
		Session session = sessionFactory.getCurrentSession();
		String mail = pret.getMembre_mail();
		Query query = session.createQuery("from membre clt where clt.membre_mail = :idmail");
		query.setParameter("idmail", mail);
		Membre mbre = (Membre) query.list().get(0);
		return mbre.getNom();
	}*/
	
	@Override
	public Pret getOpenPret(String nom) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Pret clt where clt.nom = :mail and clt.status = :statu and clt.extra = :extra");
		query.setParameter("mail", nom);
		query.setParameter("statu", "open");
		query.setParameter("extra", "no");
		@SuppressWarnings("unchecked")
		List<Pret> liste = query.list();
		if(liste.size() > 0)
			return liste.get(0);
		return null;
	}
	
	@Override
	public Pret getExtraPret(String nom) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Pret clt where clt.nom = :mail and clt.status = :statu and clt.extra = :extra");
		query.setParameter("mail", nom);
		query.setParameter("statu", "open");
		query.setParameter("extra", "yes");
		@SuppressWarnings("unchecked")
		List<Pret> liste = query.list();
		if(liste.size() > 0)
			return liste.get(0);
		return null;
	}
	@Override
	public void deletePret(int id) {
		Session session = sessionFactory.getCurrentSession();
		Pret pret = (Pret) session.get(Membre.class, id);
		if(pret != null)
			session.delete(pret);
	}

	@Override
	public int getTotalPret() {
		List<Pret> list = this.allPret();
		int total = 0;
		
		for(Pret pret : list){
			total = total + pret.getMontant();
		}
		return total;
	}

}
